package com.jmh.config;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.fill.Column;
import java.util.HashMap;
import java.util.Map;


public class CodeGenerator {
    private static String url = "jdbc:mysql://localhost:3306/sjt2203?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
    private static String userName = "root";
    private static String password = "123456";
    private static String path = "F:\\文件\\信思智学\\ssm整合框架搭建\\CodeGenerator";//文件生成路径
    private static String parentPath = "com.jmh";//包的父级路径

    public static void main(String[] args) {
        //创建数据源配置
        DataSourceConfig DATA_SOURCE_CONFIG = new DataSourceConfig.Builder(url,userName,password).build();
        AutoGenerator generator = new AutoGenerator(DATA_SOURCE_CONFIG);

        //TODO 1 配置表设置
        //1.1配置需要生成的表名
        StrategyConfig.Builder strategyConfig = new StrategyConfig.Builder().addInclude("teacher");
        //1.2过滤表前缀 例如t_user过滤为user
        generator.strategy(strategyConfig.addTablePrefix("t_","s_").build());
        //1.3过滤表后缀 deleted_flag -> deleted
        generator.strategy(strategyConfig.addFieldSuffix("_flag").build());


        //TODO 2 自动填充时间设置
        generator.strategy(strategyConfig.entityBuilder()
                .idType(IdType.ASSIGN_ID)
                .addTableFills(new Column("create_time", FieldFill.INSERT))    //基于数据库字段填充
                .addTableFills(new Column("update_time", FieldFill.INSERT_UPDATE))
                .build());

        //TODO 3 逻辑删除设置
        generator.strategy(strategyConfig.entityBuilder()
                .logicDeleteColumnName("deleted") // 基于数据库字段
                .build());

        //TODO 4.自定义文件名设置
        generator.strategy(strategyConfig
                .entityBuilder().formatFileName("%s")
                .mapperBuilder().formatMapperFileName("%sDao").formatXmlFileName("%sDao")
                .controllerBuilder().formatFileName("%sController")
                .serviceBuilder().formatServiceFileName("%sService").formatServiceImplFileName("%sServiceImpl")
                .build());


        GlobalConfig.Builder globalConfig = new GlobalConfig.Builder().fileOverride();
        //设置时间为date类型
        globalConfig.dateType(DateType.ONLY_DATE);
        //TODO 5 设置自定义路径
        Map<OutputFile, String> pathInfo = new HashMap<>();
        pathInfo.put(OutputFile.xml, path+"//mapper");
        pathInfo.put(OutputFile.entity, path+"//entity");
        pathInfo.put(OutputFile.controller, path+"//controller");
        pathInfo.put(OutputFile.mapper,path+"//dao");
        pathInfo.put(OutputFile.service, path+"//service");
        pathInfo.put(OutputFile.serviceImpl, path+"//service//impl");
        generator.strategy(strategyConfig.build());
        PackageConfig.Builder packageConfig = new PackageConfig.Builder();
        packageConfig.parent(parentPath);
        packageConfig.mapper("dao");
        generator.packageInfo(packageConfig.pathInfo(pathInfo).build());

        generator.global(globalConfig.build());

        generator.execute();

    }

}
